/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.hibernate.entities;

import com.oea.hibernate.utilities.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author unnati
 */
@Table(name = "questions")
@Entity
public class Question extends AbstractEntity
{
    
    @GeneratedValue
   @Id

    @Column(name = "id")
    protected Long id;
    
    @Column(name = "qdescription")
    protected String qdescription;
    
    @Column(name = "solution")
    protected  String solution;
    
    @Column(name = "answer")
    protected String  answer;
    
    @Column(name = "topicid")
    protected Long topicid;
    
    @Transient
    protected  String  flag;
    
    @Transient
    protected  Topic topic;
    
    @Transient
    protected String uans;

    public String getUans() {
        return uans;
    }

    public void setUans(String uans) {
        this.uans = uans;
    }
    
    
    
    
    public Topic getTopic() {
        return topic;
    }

    public void setTopic( ) {
        this.topic = (Topic) new Topic().getById(topicid);
    }
    
    

    public String getQdescription() {
        return qdescription;
    }

    public void setQdescription(String qdescription) {
        this.qdescription = qdescription;
    }

    public String getSolution() {
        return solution;
    }

    public void setSolution(String solution) {
        this.solution = solution;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

  

    public Long getTopicid() {
        return topicid;
    }

    public void setTopicid(Long topicid) {
        this.topicid = topicid;
    }

  

   

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag() {
        this.flag = "1";
    }

    @Override
    public int hashCode() {
        return super.hashCode(); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean equals(Object o) {
        //return super.equals(o); //To change body of generated methods, choose Tools | Templates.
        boolean flag=false;
       Test_Question  q=(Test_Question)o;
        if(this.id==q.getQid())
        {
            flag=true;
        }
        
        return flag;
    }

   
    
    
     public static List<Question> getallById(Long topic_id) {
        List<Question> li = null;
        try 
        {
            Session session = HibernateUtil.getSessionFactory().openSession();
            
            Query q=session.createQuery("from Question where topicid='"+topic_id+"'");
            li=(ArrayList<Question>) q.list();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return li;
    }
    
     public static List<Question> GetAllQuestionBYTopicID(Long subid)
     {
         List<Question> li_question_by_topicid=null;
         
         try 
         {
             Session s=HibernateUtil.getSessionFactory().openSession();
             Query q=s.createQuery("from Question where topicid in (select id from Topic where subid="+subid+")");
             li_question_by_topicid=q.list();
         }
         catch (Exception e) 
         {
             e.printStackTrace();
         }
         
         return  li_question_by_topicid;
     }
     
     public  static List<Question> GetAllAddedQuestion(Long test_id)
     {
         List<Question> li_added_test_question=null;
         
         try 
         {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Query q=s.createQuery("from Question where id in (select qid from Test_Question where tid="+test_id+")");
            li_added_test_question=q.list();
             System.out.println(q.list().size());
             System.out.println("ro");
         } 
         catch (Exception e) 
         {
             e.printStackTrace();
         }
         
         return li_added_test_question;
     }
     public static Long GetTopicid(long  qid)
     {
         Long l=null;
         
         try 
         {
             Session s=HibernateUtil.getSessionFactory().openSession();
             Query q=s.createQuery("select topicid from Question where id="+qid+"");
             l=(Long) q.list().get(0);
         } 
         catch (Exception e) 
         {
             e.printStackTrace();
         }
         return l;
     }
     
     public static String GetAnswer(long  qid)
     {
         String str=null;
         
         try 
         {
             Session s=HibernateUtil.getSessionFactory().openSession();
             Query q=s.createQuery("select answer from Question where id="+qid+"");
             str=(String) q.list().get(0);
         } 
         catch (Exception e) 
         {
             e.printStackTrace();
         }
         return str;
     }
     
    
}
